import {AxiosOptions} from "../@type";
import {addPadding, removePadding} from "../plugin/padding";
import {AxiosResponse} from "axios";

/** 发起请求拦截 */
export const requestInterceptors = (request:AxiosOptions) => {
    addPadding(request)
    return request
}


/** 发起请求失败 */
export const requestInterceptorsError = (error: any, fail: ((error: any) => any) | undefined, complete: ((info: any) => any) | undefined) => {
    // console.log(error)
    // return error
}


/** 成功响应 */
export const responseInterceptors = (response: AxiosResponse, success: ((data: any) => void) | undefined, fail: ((error: any) => void) | undefined, complete: ((data: any) => void) | undefined ) => {
    const { data } = response
    removePadding(<AxiosOptions>response.config)
    const successData = success?.(data)
    // if(status!==1 || status!=='1') return responseInterceptorsError( data,fail,complete)
    complete?.(data)
    return successData || data
}

/** 响应失败 根据status进行验证判断 */
export const responseInterceptorsError = (error: any, fail: ((error: any) => void) | undefined, complete: ((error: any) => void) | undefined) => {
    const failData = fail?.(error)
    complete?.(error)
    return  failData || error
}